package com.airforce.controller;


import com.airforce.model.wechat_user;
import com.airforce.service.getShareInfo;
import com.airforce.utils.JsonUtil;
import com.airforce.utils.MD5BASE64Util;
import com.airforce.utils.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping(value = "app/user/")
@RestController
public class Wehar_userCon_app {

    @Autowired
    com.airforce.mappers.wechat_userMapper wechat_userMapper;


    private  final static Logger logger=LoggerFactory.getLogger(wechatLogin.class);

    //查询用户信息
    @GetMapping(value = "info")
    public Object appuser(@RequestParam String token){

        String[] str = MD5BASE64Util.base64decode(token).split(",");
        String openid=str[1];
        wechat_user user=wechat_userMapper.selectByopenid(openid);
        return ResultCode.SUCCESS.result(user);

    }


    //分享更新抽奖次数
//    @GetMapping("lotterynum")
//    public Object lotterynumupdata(String ShareNickName){
//
//        String name=java.net.URLEncoder.encode(ShareNickName);
//        List<wechat_user> testuser=wechat_userMapper.selectBylist(name);
//
//            int sharenum = testuser.get(0).getSharenum();
//            int lotterynum = testuser.get(0).getLotterynum();
//            if (sharenum >= 0 && sharenum < 2) {
//                logger.info(ShareNickName + "用户第" + (sharenum + 1) + "次分享，增加用户抽奖次数");
//                wechat_user userparm = new wechat_user();
//                userparm.setOpenid(name);
//                userparm.setShare((byte) 1);
//                userparm.setSharenum(sharenum + 1);
//                userparm.setLotterynum(lotterynum + 1);
//                wechat_userMapper.updateBysharmadd(userparm);
//                return true;
//            } else {
//                logger.info("用户已分享过2次以上，不增加抽奖次数~");
//                return false;
//            }
//
//    }

    @GetMapping("getShareInfo")
    public Object getShare(@RequestParam String encryptedData,@RequestParam String iv,@RequestParam String token){
        if(token!=null) {
            String[] str = MD5BASE64Util.base64decode(token).split(",");
            String sesion_key = str[0];

            getShareInfo getShareInfo = new getShareInfo();
            String openGid = getShareInfo.get(encryptedData, sesion_key, iv);

            System.out.println("进行了群分享");

            return ResultCode.SUCCESS.result(openGid);
        }else {
            return ResultCode.NO_PARM.result();
        }

    }

}
